package cn.sytton.taffe.code.controller;

import cn.sytton.taffe.code.entity.CodeEntity;
import cn.sytton.taffe.code.service.CodeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 自动代码生成控制层
 *
 * @author skyrock
 */
@Controller
@RequiredArgsConstructor
@RequestMapping("/code")
public class CodeController {

    @Value("${code.java-src-path}")
    private String javaSrcPath;

    @Value("${code.vue-src-path}")
    private String jsSrcPath;

    @Resource
    private final CodeService codeService;

    @ResponseBody
    @PostMapping
    public Map<String, String> generate(@RequestBody CodeEntity entity) {
        return codeService.generate(entity);
    }

    @GetMapping
    public ModelAndView page(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("javaPath", javaSrcPath);
        modelAndView.addObject("jsPath", jsSrcPath);
        modelAndView.setViewName("code");
        return modelAndView;
    }
}
